<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .error {
            background: red;
            color: white;
        }

        .success {
            background: green;
            color: white;
        }
    </style>
</head>

<body>
    <form>
        <fieldset>
            <legend>注册</legend>
            <p>
                用户 <input type="text" name="username">
                <span id="usernameError" style="display: none;">用户名必须填写</span>
            </p>
            <p>
                邮箱 <input type="text" name="email">
                <span id="emailError" style="display: none;">邮箱必须填写</span>
            </p>
        </fieldset>
    </form>
    <script>
        // 表单验证
        // 用户名
        // 密码
        // 确认密码
        // 邮箱
        // 手机
        document.oninput = function (e) {
            const target = e.target;
            const name = target.name;
            const value = target.value;
            if (name === 'username') {
                if (value === '') {
                    target.classList.add('error')
                    target.classList.remove('success')
                    usernameError.style.display = ''
                } else {
                    target.classList.add('success')

                    target.classList.remove('error')
                    usernameError.style.display = 'none'
                }
            }

            if (name === 'email') {
                if (value === '') {
                    emailError.style.display = ''
                } else {
                    emailError.style.display = 'none'
                }
            }
        }

    </script>
</body>

</html>